import 'dart:convert';

import 'package:flutter/material.dart';

import '../../../common/base_config.dart';
import '../../../common/system_config.dart';

class TextFieldWidget {
  // 上下文
  static BuildContext ct;

  // 用户名、密码输入框的控制器
  static TextEditingController account = TextEditingController();
  static TextEditingController password = TextEditingController();

  static final _accountFocus = FocusNode();
  static final _passwordFocus = FocusNode();

  /// 构建用户名，密码输入框
  /// * [context] 上下文
  /// * [platformInfo] 平台信息
  static Widget build(
      BuildContext context, bool platformInfo, String loginInfo) {
    if (loginInfo != null) {
      Map m = json.decode(loginInfo);
      account.text = m["account"];
      password.text = m["password"];
    }

    ct = context;
    return Column(
      children:
          platformInfo ? _structureAndroidWidget() : _structureIosWidget(),
    );
  }

  // 针对android绘制输入框并添加行为处理
  static List<Widget> _structureAndroidWidget() {
    List<Widget> lw = [];
    lw = [
      Container(
        child: Listener(
          onPointerDown: (e) => FocusScope.of(ct).requestFocus(_accountFocus),
          child: TextField(
            controller: account,
            focusNode: _accountFocus,
            keyboardType: TextInputType.text,
            textInputAction: TextInputAction.done,
            keyboardAppearance: Brightness.light,
            decoration: InputDecoration(
              hintText: '请输入用户名',
              hintStyle: TextStyle(fontSize: SystemConfig.fs_title3),
              prefixIcon: Icon(Icons.account_box),
            ),
          ),
        ),
      ),
      Container(
        child: Listener(
          onPointerDown: (e) => FocusScope.of(ct).requestFocus(_passwordFocus),
          child: TextField(
            controller: password,
            focusNode: _passwordFocus,
            keyboardType: TextInputType.text,
            textInputAction: TextInputAction.done,
            keyboardAppearance: Brightness.light,
            decoration: InputDecoration(
              hintText: '请输入密码',
              hintStyle: TextStyle(fontSize: SystemConfig.fs_title3),
              prefixIcon: Icon(Icons.keyboard),
            ),
            obscureText: true,
          ),
        ),
      )
    ];

    return lw;
  }

  // 针对ios绘制输入框并添加行为处理
  static List<Widget> _structureIosWidget() {
    List<Widget> lw = [];
    lw = [
      Container(
        child: TextField(
          controller: account,
          focusNode: _accountFocus,
          keyboardType: TextInputType.text,
          textInputAction: TextInputAction.done,
          keyboardAppearance: Brightness.light,
          decoration: InputDecoration(
            hintText: '请输入用户名',
            prefixIcon: Icon(Icons.account_box),
          ),
        ),
      ),
      Container(
        child: TextField(
          controller: password,
          focusNode: _passwordFocus,
          keyboardType: TextInputType.text,
          textInputAction: TextInputAction.done,
          keyboardAppearance: Brightness.light,
          decoration: InputDecoration(
            hintText: '请输入密码',
            prefixIcon: Icon(Icons.keyboard),
          ),
          obscureText: true,
        ),
      ),
    ];
    return lw;
  }

  static Map verificaLoginInfo() {
    RegExp regExp = RegExp(BaseConfig.Reg);

    // 验证用户名
    // !regExp.hasMatch(account.text)
    if (account.text == null || account.text.trim().length < 5) {
      return {"state": false, "msg": "用户名输入有误"};
    }

    // 验证密码
    if (password.text == null || password.text.length <= 5) {
      return {"state": false, "msg": "密码输入有误"};
    }

    return {
      "state": true,
      "data": {
        "account": account.text,
        "password": password.text,
        "type": 2,
      },
      "msg": "通过"
    };
  }
}
